<?php
namespace app\beautiful\controller\admin;
use app\beautiful\model\logs\MemberBalanceLogsModel;
use app\beautiful\model\logs\MemberCouponLogsModel;
use app\beautiful\model\logs\MemberIntegralLogsModel;
use app\beautiful\model\member\MemberModel;
use app\BaseController;
use app\beautiful\model\member\MemberSnapUpModel;
use app\beautiful\model\order\BalanceOrderModel;
use app\beautiful\model\order\BuyOrderModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\order\OnceCardOrderModel;
use app\common\logic\KefuLogic;
use app\common\model\OrderModel;
use app\yoga\model\appointment\SignupModel;
use think\facade\Cache;

class Home extends BaseController{
    public function index()
    {
        //商铺信息
        $this->request->shop->append(['expire_time_format','is_expire']);
        $shop=[
            'type'=>$this->request->shop->type,
            'expire_time_format'=>$this->request->shop->expire_time_format,
            'is_expire'=>$this->request->shop->is_expire,
        ];

        //客服
        $kefu = KefuLogic::getKefu($this->request->shopId);

        //会员余额储值
        if(!Cache::get('base_shop_'.$this->request->shopId.'_home_balance_recharge_data')){
            $total_recharge_balance = $total_recharge_give_balance = 0;
            $today_recharge_balance = $today_recharge_give_balance = 0;
            $this_month_recharge_balance = $this_month_recharge_give_balance=0;
            $total_recharge_balance_data = MemberBalanceLogsModel::where([
                ['shop_id','=',$this->request->shopId],
                ['type','=',1]
            ])->field('shop_id,sum(balance) as balance,sum(give_balance) as give_balance')
                ->group('shop_id')->select()->toArray();

            $today_recharge_balance_data = MemberBalanceLogsModel::where([
                ['shop_id','=',$this->request->shopId],
                ['type','=',1],
            ])->whereDay('add_time')
                ->field('shop_id,sum(balance) as balance,sum(give_balance) as give_balance')
                ->group('shop_id')->select()->toArray();

            $this_month_recharge_balance_data = MemberBalanceLogsModel::where([
                ['shop_id','=',$this->request->shopId],
                ['type','=',1],
            ])->whereMonth('add_time')
                ->field('shop_id,sum(balance) as balance,sum(give_balance) as give_balance')
                ->group('shop_id')->select()->toArray();

            if(!empty($total_recharge_balance_data)){
                //累计
                $total_recharge_balance=$total_recharge_balance_data[0]['balance'];
                //累计赠送的余额
                $total_recharge_give_balance=$total_recharge_balance_data[0]['give_balance'];
            }
            if(!empty($today_recharge_balance_data)){
                //今日金额
                $today_recharge_balance=$today_recharge_balance_data[0]['balance'];
                //今日赠送的金额
                $today_recharge_give_balance=$today_recharge_balance_data[0]['give_balance'];
            }
            if(!empty($this_month_recharge_balance_data)){
                //本月金额
                $this_month_recharge_balance=$this_month_recharge_balance_data[0]['balance'];
                //本月赠送的金额
                $this_month_recharge_give_balance=$this_month_recharge_balance_data[0]['give_balance'];
            }
            $balance_recharge_data=[
                'total_recharge_balance'=>$total_recharge_balance,
                'total_recharge_give_balance'=>$total_recharge_give_balance,
                'today_recharge_balance'=>$today_recharge_balance,
                'today_recharge_give_balance'=>$today_recharge_give_balance,
                'this_month_recharge_balance'=>$this_month_recharge_balance,
                'this_month_recharge_give_balance'=>$this_month_recharge_give_balance,
            ];
            Cache::set('base_shop_'.$this->request->shopId.'_home_balance_recharge_data',$balance_recharge_data,600);
        }else{
            $balance_recharge_data=Cache::get('base_shop_'.$this->request->shopId.'_home_balance_recharge_data');
        }


        if(!Cache::get('base_shop_'.$this->request->shopId.'_home_balance_consume_data')){
            //会员余额核销
            $total_consume_balance = MemberBalanceLogsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['type','=',-1]
                ])->sum('balance') ?? 0;

            $today_consume_balance = MemberBalanceLogsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['type','=',-1],
                ])->whereDay('add_time')->sum('balance') ?? 0;

            $this_month_consume_balance = MemberBalanceLogsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['type','=',-1],
                ])->whereMonth('add_time')->sum('balance') ?? 0;
            $balance_consume_data=[
                'total_consume_balance'=>$total_consume_balance,
                'today_consume_balance'=>$today_consume_balance,
                'this_month_consume_balance'=>$this_month_consume_balance,
            ];
            Cache::set('base_shop_'.$this->request->shopId.'_home_balance_consume_data',$balance_consume_data,600);
        }else{
            $balance_consume_data=Cache::get('base_shop_'.$this->request->shopId.'_home_balance_consume_data');
        }


        if(!Cache::get('base_shop_'.$this->request->shopId.'_home_coupon_consume_data')){
            //优惠券使用张数
            $total_consume_coupon = MemberCouponLogsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['type','=',-1]
                ])->count() ?? 0;

            $today_consume_coupon = MemberCouponLogsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['type','=',-1],
                ])->whereDay('add_time')->count() ?? 0;

            $this_month_consume_coupon = MemberCouponLogsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['type','=',-1],
                ])->whereMonth('add_time')->count() ?? 0;
            $coupon_consume_data=[
                'total_consume_coupon'=>$total_consume_coupon,
                'today_consume_coupon'=>$today_consume_coupon,
                'this_month_consume_coupon'=>$this_month_consume_coupon,
            ];
            Cache::set('base_shop_'.$this->request->shopId.'_home_coupon_consume_data',$coupon_consume_data,600);
        }else{
            $coupon_consume_data=Cache::get('base_shop_'.$this->request->shopId.'_home_coupon_consume_data');
        }


        //充值会员排行榜
        $balance_time_type=$this->request->param('balance_time_type/d',1);
        $member_balance_logs_model = new MemberBalanceLogsModel();
        if($balance_time_type == 1) $member_balance_logs_model = $member_balance_logs_model->whereMonth('add_time');
        if($balance_time_type == 2) $member_balance_logs_model = $member_balance_logs_model->whereYear('add_time');
        $balance_rank_data = $member_balance_logs_model->where([
            ['shop_id','=',$this->request->shopId],
            ['type','=',1],
        ])->field('member_id,sum(balance) as balance,sum(give_balance) as give_balance')
            ->group('member_id')->order(['balance'=>'desc','give_balance'=>'desc'])
            ->limit(5)->select()->toArray();
        if(!empty($balance_rank_data)){
            $memberIds =  [];
            foreach($balance_rank_data as $val){
                $memberIds[$val['member_id']] = $val['member_id'];
            }
            if(!empty($memberIds)){
                $memberModel = new MemberModel();
                $members =  $memberModel->getMemberInfoByMemberId($memberIds);
            }
            foreach ($balance_rank_data as $k => $list){
                $balance_rank_data[$k]['member']=$members[$list['member_id']] ?? [];
                $balance_rank_data[$k]['rank']=$k+1;
            }
        }

        //会员积分排行榜
        $integral_time_type=$this->request->param('integral_time_type/d',1);
        $member_integral_logs_model = new MemberIntegralLogsModel();
        if($integral_time_type == 1) $member_integral_logs_model = $member_integral_logs_model->whereMonth('add_time');
        if($integral_time_type == 2) $member_integral_logs_model = $member_integral_logs_model->whereYear('add_time');
        $integral_rank_data = $member_integral_logs_model->where([
            ['shop_id','=',$this->request->shopId],
            ['type','=',1],
        ])->field('member_id,sum(integral) as integral')
            ->group('member_id')->order(['integral'=>'desc'])
            ->limit(5)->select()->toArray();
        if(!empty($integral_rank_data)){
            $memberIds =  [];
            foreach($integral_rank_data as $val){
                $memberIds[$val['member_id']] = $val['member_id'];
            }
            if(!empty($memberIds)){
                $memberModel = new MemberModel();
                $members =  $memberModel->getMemberInfoByMemberId($memberIds);
            }
            foreach ($integral_rank_data as $k => $list){
                $integral_rank_data[$k]['member']=$members[$list['member_id']] ?? [];
                $integral_rank_data[$k]['rank']=$k+1;
            }
        }

        // 购买抢购 Start -----------------------------------------------------------------------------------------------
        $signup_total_consume_balance = MemberSnapUpModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',0]
            ])->sum('price') ?? 0;
        $signup_today_consume_balance = MemberSnapUpModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',0]
            ])->whereDay('add_time')->sum('price') ?? 0;
        $signup_today_consume_num = MemberSnapUpModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',0]
            ])->whereDay('add_time')->count() ?? 0;
        $signup_this_month_consume_balance = MemberSnapUpModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',0]
            ])->whereMonth('add_time')->sum('price') ?? 0;
        $signup_this_month_consume_num = MemberSnapUpModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',0]
            ])->whereMonth('add_time')->count() ?? 0;
        $signup_sale_data = [
            'signup_total_consume_balance'=>$signup_total_consume_balance,
            'signup_today_consume_balance'=>$signup_today_consume_balance,
            'signup_today_consume_num'=>$signup_today_consume_num,
            'signup_this_month_consume_balance'=>$signup_this_month_consume_balance,
            'signup_this_month_consume_num'=>$signup_this_month_consume_num,
        ];
        // 购买抢购 End -------------------------------------------------------------------------------------------------

        // 购买次卡 Start -----------------------------------------------------------------------------------------------
        $once_card_total_consume_balance = OnceCardOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->sum('total_price') ?? 0;
        $once_card_today_consume_balance = OnceCardOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereDay('add_time')->sum('total_price') ?? 0;
        $once_card_total_consume_num = OnceCardOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereDay('add_time')->count() ?? 0;
        $once_card_this_month_consume_balance = OnceCardOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereMonth('add_time')->sum('total_price') ?? 0;
        $once_card_this_month_consume_num = OnceCardOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereMonth('add_time')->count() ?? 0;
        $once_card_data = [
            'once_card_total_consume_balance'=>$once_card_total_consume_balance,
            'once_card_today_consume_balance'=>$once_card_today_consume_balance,
            'once_card_total_consume_num'=>$once_card_total_consume_num,
            'once_card_this_month_consume_balance'=>$once_card_this_month_consume_balance,
            'once_card_this_month_consume_num'=>$once_card_this_month_consume_num,
        ];
        // 购买次卡 End -------------------------------------------------------------------------------------------------

        // 购买商品 Start -----------------------------------------------------------------------------------------------
        $goods_order_total_consume_balance = GoodsOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1],
                ['status','<>',1]
            ])->sum('need_pay') ?? 0;
        $goods_order_today_consume_balance = GoodsOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1],
                ['status','<>',1]
            ])->whereDay('add_time')->sum('need_pay') ?? 0;
        $goods_order_total_consume_num = GoodsOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1],
                ['status','<>',1]
            ])->whereDay('add_time')->count() ?? 0;
        $goods_order_this_month_consume_balance = GoodsOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1],
                ['status','<>',1]
            ])->whereMonth('add_time')->sum('need_pay') ?? 0;
        $goods_order_this_month_consume_num = GoodsOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1],
                ['status','<>',1]
            ])->whereMonth('add_time')->count() ?? 0;
        $goods_order_data = [
            'goods_order_total_consume_balance'=>$goods_order_total_consume_balance,
            'goods_order_today_consume_balance'=>$goods_order_today_consume_balance,
            'goods_order_total_consume_num'=>$goods_order_total_consume_num,
            'goods_order_this_month_consume_balance'=>$goods_order_this_month_consume_balance,
            'goods_order_this_month_consume_num'=>$goods_order_this_month_consume_num,
        ];
        // 购买商品 End -------------------------------------------------------------------------------------------------

        // 购买商品 Start -----------------------------------------------------------------------------------------------
        $buy_order_total_consume_balance = BuyOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->sum('balance') ?? 0;
        $buy_order_today_consume_balance = BuyOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereDay('add_time')->sum('balance') ?? 0;
        $buy_order_total_consume_num = BuyOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereDay('add_time')->count() ?? 0;
        $buy_order_this_month_consume_balance = BuyOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereMonth('add_time')->sum('balance') ?? 0;
        $buy_order_this_month_consume_num = BuyOrderModel::where([
                ['shop_id','=',$this->request->shopId],
                ['is_paid','=',1]
            ])->whereMonth('add_time')->count() ?? 0;
        $buy_order_data = [
            'buy_order_total_consume_balance'=>$buy_order_total_consume_balance,
            'buy_order_today_consume_balance'=>$buy_order_today_consume_balance,
            'buy_order_total_consume_num'=>$buy_order_total_consume_num,
            'buy_order_this_month_consume_balance'=>$buy_order_this_month_consume_balance,
            'buy_order_this_month_consume_num'=>$buy_order_this_month_consume_num,
        ];

        $return=[
            'shop'=>$shop,
            'kefu'=>$kefu,
            'balance_recharge_data'=>$balance_recharge_data, // 会员余额储值
            'balance_consume_data'=>$balance_consume_data, // 会员余额核销
            'coupon_consume_data'=>$coupon_consume_data, // 优惠券使用张数
            'signup_sale_data'=>$signup_sale_data, // 抢购
            'once_card_data'=>$once_card_data, // 次卡
            'goods_order_data'=>$goods_order_data, // 商品
            'buy_order_data'=>$buy_order_data, // 买单
            'balance_rank_data'=>$balance_rank_data,
            'integral_rank_data'=>$integral_rank_data
        ];
        return $this->success($return);
    }
}
